package infostroy.bookagolf.components.dialogs;

import com.academysmart.bookagolf.encryption.GolfApi;
import com.academysmart.bookagolf.i18n.Messages;
import com.academysmart.bookagolf.model.BlockDate;
import com.academysmart.bookagolf.provider.ClubProvider;
import com.academysmart.bookagolf.provider.FieldProvider;
import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.common.Constants.Dimensions;
import infostroy.bookagolf.common.Constants.Icons;
import infostroy.bookagolf.common.helpers.ComponentsHelper;
import infostroy.bookagolf.common.helpers.ImageHelper;
import infostroy.bookagolf.common.helpers.StringHelper;
import infostroy.bookagolf.components.controls.GolfRoundButton;
import infostroy.bookagolf.components.inputs.GolfComboBox;
import infostroy.bookagolf.components.inputs.GolfTextArea;
import infostroy.bookagolf.components.panels.GolfRoundPanel;
import infostroy.bookagolf.components.widgets.GolfCalendar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by valer_000 on 4/2/14.
 */
public class GolfBlockDateDialog extends GolfBaseDialog {

    private static final String DATE = Messages.AddOrderView_Date;
    private static final String FROM = Messages.StatisticsFrom;
    private static final String TO = Messages.StatisticsTo;
    private static final String FIELD_NAME = Messages.Order_golfField;
    private static final String DESCRIPTION = Messages.CompanyInfo_Description;
    private static final String SAVE = Messages.CompanyInfo_Save;

    private JPanel holderPanel;
    private JLabel chosenDateLbl;

    private JComboBox fromJCB = new GolfComboBox();
    private JComboBox toJCB = new GolfComboBox();
    private JComboBox fieldJCB = new GolfComboBox();

    private JTextArea descriptionJTA = new GolfTextArea();

    private Calendar calendar = Calendar.getInstance();
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");


    public GolfBlockDateDialog(String title, Color background, Color titleColor) {
        super(title, background, titleColor);
    }

    public GolfBlockDateDialog(String title, Color background, Dimension dimension, Color titleColor) {
        super(title, background, dimension, titleColor);
        holderPanel = new JPanel();
    }

    @Override
    protected void initContent() {
        holderPanel.setLayout(new BoxLayout(holderPanel, BoxLayout.PAGE_AXIS));
        holderPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        holderPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
        holderPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        holderPanel.add(initDateBlock());
        holderPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        holderPanel.add(initTimeRangeBlock());
        holderPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        holderPanel.add(initFieldBlock());
        holderPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        holderPanel.add(initDescriptionBlock());
        holderPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        holderPanel.add(initButtonBlock());
        contentPanel.add(holderPanel);
    }

    private JPanel initDateBlock() {
        JPanel datePanel = new GolfRoundPanel();
        datePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        datePanel.setLayout(new BoxLayout(datePanel, BoxLayout.LINE_AXIS));
        datePanel.setPreferredSize(Dimensions.SLIDER_PANEL_ROW_DIM);
        datePanel.setMaximumSize(Dimensions.SLIDER_PANEL_ROW_DIM);
        datePanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
        chosenDateLbl = new JLabel();
        chosenDateLbl.setText(dateFormat.format(calendar.getTime()));
        datePanel.add(chosenDateLbl);
        datePanel.add(Box.createHorizontalGlue());
        JButton calendarButton = ComponentsHelper.createButtonWithImg(ImageHelper
                .getInstance().loadImage(Icons.CALENDAR_ICON));
        final GolfCalendar calendarWidget = new GolfCalendar(calendarButton, new BlockDateCalendarCB());
        calendarButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                calendarWidget.show();
            }
        });
        datePanel.add(calendarButton);
        JPanel panel = new JPanel(new BorderLayout());
        panel.setMaximumSize(new Dimension(dimension.width, 50));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.add(new JLabel(DATE), BorderLayout.NORTH);
        panel.add(datePanel, BorderLayout.SOUTH);
        return panel;
    }

    private JPanel initTimeRangeBlock() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setMaximumSize(new Dimension(dimension.width, 50));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel labelsPanel = new JPanel(new GridLayout(1, 2, 20, 0));
        labelsPanel.add(new JLabel(FROM));
        labelsPanel.add(new JLabel(TO));

        JPanel controlsPanel = new JPanel(new GridLayout(1, 2, 20, 0));
        fromJCB.setModel(new DefaultComboBoxModel(StringHelper.getHourArray()));
        toJCB.setModel(new DefaultComboBoxModel(StringHelper.getHourArray()));
        controlsPanel.add(fromJCB);
        controlsPanel.add(toJCB);

        panel.add(labelsPanel, BorderLayout.NORTH);
        panel.add(controlsPanel, BorderLayout.SOUTH);
        return panel;
    }

    private JPanel initFieldBlock() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setMaximumSize(new Dimension(dimension.width, 50));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.add(new JLabel(FIELD_NAME), BorderLayout.NORTH);
        fieldJCB.setModel(new DefaultComboBoxModel(FieldProvider
                .INSTANCE.getAllFieldNames()));
        panel.add(fieldJCB, BorderLayout.SOUTH);
        return panel;
    }

    private JPanel initDescriptionBlock() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setMaximumSize(new Dimension(dimension.width, 110));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.add(new JLabel(DESCRIPTION), BorderLayout.NORTH);
        descriptionJTA.setRows(5);
        descriptionJTA.validate();
        panel.add(descriptionJTA, BorderLayout.SOUTH);
        return panel;
    }

    private JPanel initButtonBlock() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        JButton saveBtn = new GolfRoundButton(SAVE, Colors.BLUE_BUTTON_BACKGROUND
                , Colors.BUTTON_FOREGROUND, Colors.BLUE_BUTTON_BORDER, Dimensions.BLUE_BUTTON_DIM );
        saveBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //TODO:Block dates
                try {
                    Calendar cal = Calendar.getInstance();
                    cal.set(Calendar.MINUTE, 0);

                    BlockDate blockDate = new BlockDate();
                    blockDate.setBlockDate(calendar.getTime());
                    cal.set(Calendar.HOUR, Integer.parseInt(fromJCB.getSelectedItem().toString()));
                    blockDate.setFrom(cal.getTime());
                    cal.set(Calendar.HOUR, Integer.parseInt(toJCB.getSelectedItem().toString()));
                    blockDate.setTo(cal.getTime());
                    blockDate.setDescription(descriptionJTA.getText());
                    GolfApi api = new GolfApi();
                    api.blockDate(ClubProvider.getClub().getId(),
                            StringHelper.getFieldByName(fieldJCB.getSelectedItem().toString()).getId(), blockDate);
                    dialog.setVisible(false);
                } catch (Exception ex) {
                    //TODO: show error dialog
                    ex.printStackTrace();
                    dialog.setVisible(false);
                }
            }
        });
        panel.add(saveBtn);
        return panel;
    }

    private class BlockDateCalendarCB implements GolfCalendar.CalendarCallback {

        @Override
        public void updateDate(Date date) {
            chosenDateLbl.setText(dateFormat.format(date));
            calendar.setTime(date);
        }
    }
}
